from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 封装添加工作站函数
def add_workstation(workstation_data):
    # 跳转至新建工作站界面
    browser.find_element(By.ID, "mainmenua_mrp").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_workstation_workstation_card .vsmenu").click()

    # 标签
    browser.find_element(By.ID, "label").send_keys(workstation_data["label"])
    # 类型(只能写2)
    browser.find_element(By.CSS_SELECTOR, f"select#type>option:nth-child({workstation_data['type']})").click()
    # 需要的操作人员数量
    browser.find_element(By.ID, "nb_operators_required").send_keys(str(workstation_data["nb_operators_required"]))
    # Estimated operator THM
    browser.find_element(By.ID, "thm_operator_estimated").send_keys(str(workstation_data["thm_operator_estimated"]))
    # Estimated machine THM
    browser.find_element(By.ID, "thm_machine_estimated").send_keys(str(workstation_data["thm_machine_estimated"]))
    # 状态（只能写3）
    browser.find_element(By.CSS_SELECTOR, f"select#status>option:nth-child({workstation_data['status']})").click()


    # 创建按钮
    browser.find_element(By.NAME, "add").click()

if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        # 添加工作站功能测试数据（5个用例）
        #测试结果：正向与预期完全一致，反向测试除了第一个和预期一致，后面两个都添加成功
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "case_desc": "添加标准工作站",
                "label": "装配工作站-A",
                "type": 2,  # 仅允许值2
                "nb_operators_required": 5,  # 标准操作人员数量
                "thm_operator_estimated": 800,  # 标准操作员THM
                "thm_machine_estimated": 3000,  # 标准机器THM
                "status": 3,  # 仅允许值3
                "group": 2,  # 允许值1,2,3
                "expected_result": "工作站添加成功，显示成功提示"
            },
            {
                "case_desc": "添加最小配置工作站",
                "label": "测试工作站-B",
                "type": 2,
                "nb_operators_required": 1,  # 最小操作人员数量
                "thm_operator_estimated": 1,  # 最小操作员THM
                "thm_machine_estimated": 1,  # 最小机器THM
                "status": 3,
                "group": 1,
                "expected_result": "工作站添加成功，显示成功提示"
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "case_desc": "空标签测试",
                "label": "",  # 空标签
                "type": 2,
                "nb_operators_required": 5,
                "thm_operator_estimated": 800,
                "thm_machine_estimated": 3000,
                "status": 3,
                "group": 2,
                "expected_result": "提示'标签不能为空'，工作站未创建"
            },
            {
                "case_desc": "无效类型测试",
                "label": "无效类型工作站",
                "type": 1,  # 非法类型值
                "nb_operators_required": 5,
                "thm_operator_estimated": 800,
                "thm_machine_estimated": 3000,
                "status": 3,
                "group": 2,
                "expected_result": "提示'请选择有效类型'，工作站未创建"
            },
            {
                "case_desc": "负数操作人员数量测试",
                "label": "负数测试工作站",
                "type": 2,
                "nb_operators_required": -5,  # 非法负数
                "thm_operator_estimated": 800,
                "thm_machine_estimated": 3000,
                "status": 3,
                "group": 2,
                "expected_result": "提示'操作人员数量必须大于0'，工作站未创建"
            }
        ]
        for data in test_data:
            add_workstation(data)
            sleep(1)  # 等待操作完成
    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()